package com.jtemimal.utils;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

public class FileColorUtil {
    // ANSI 颜色代码
    private static final String RESET = "\u001B[0m";
    private static final String BLUE = "\u001B[34m";    // 目录
    private static final String GREEN = "\u001B[32m";   // 可执行文件
    private static final String CYAN = "\u001B[36m";    // 符号链接
    private static final String YELLOW = "\u001B[33m";  // 设备文件
    private static final String WHITE = "\u001B[37m";   // 普通文件

    public static String colorize(File file) {
        String name = file.getName();
        
        if (file.isDirectory()) {
            return BLUE + name + "/" + RESET;
        }
        
        if (file.canExecute()) {
            return GREEN + name + "*" + RESET;
        }
        
        if (Files.isSymbolicLink(file.toPath())) {
            return CYAN + name + "@" + RESET;
        }
        
        // 根据文件扩展名设置颜色
        String extension = getFileExtension(name);
        switch (extension.toLowerCase()) {
            case "zip":
            case "tar":
            case "gz":
            case "rar":
                return YELLOW + name + RESET;
            default:
                return WHITE + name + RESET;
        }
    }

    private static String getFileExtension(String filename) {
        int lastDotIndex = filename.lastIndexOf('.');
        if (lastDotIndex > 0) {
            return filename.substring(lastDotIndex + 1);
        }
        return "";
    }
} 